package com.mlz.mlzmall.ui.activity.act.personal.userLogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyCenterUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.RequestUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.utils.vali.AbStrUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @description 注册2界面
 * Created by 安帅 on 2016/5/24 11:47.
 */
public class RegistTwoActivity extends VolleyBaseActivity implements View.OnClickListener {
    private static final String TGA = "RegistTwoActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist_get_number2);
        assignViews();
        setViewContent();
        setViewClickLinstener();
        initData();

    }

    private TopTitleView registTopTitleTwo;
    private EditText etPassword1;
    private EditText etPassword2;
    private Button btnLastZhuce;
    private TextView tvGoZhuce;

    private void assignViews() {
        registTopTitleTwo = (TopTitleView) findViewById(R.id.regist_top_title_two);
        etPassword1 = (EditText) findViewById(R.id.et_password1);
        etPassword2 = (EditText) findViewById(R.id.et_password2);
        btnLastZhuce = (Button) findViewById(R.id.btn_last_zhuce);
        tvGoZhuce = (TextView) findViewById(R.id.tv_go_zhuce);
    }

    private void setViewContent() {
        registTopTitleTwo.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        registTopTitleTwo.setTitleButtonContent("注册", null, null);

    }

    private void setViewClickLinstener() {
        registTopTitleTwo.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
            }
        });
        btnLastZhuce.setOnClickListener(this);
        tvGoZhuce.setOnClickListener(this);
    }

    private String pwdone, pwdtwo, loginName, code;

    private void initData() {
        loginName = getIntent().getStringExtra(RegistActivity.LOGINNAME);
        code = getIntent().getStringExtra(RegistActivity.USERCODE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_last_zhuce:
                pwdone = etPassword1.getText().toString().trim();
                pwdtwo = etPassword2.getText().toString().trim();
                if (pwdone == null || pwdtwo == null) {
                    MyCenterUtils.ToastText("密码不能为空");
                } else {
                    if (!pwdone.equals(pwdtwo)) {
                        MyCenterUtils.ToastText("两次密码不一致");
                        return;
                    }
                }
                if (!AbStrUtil.isPassword(pwdone)) {
                    MyCenterUtils.ToastText("密码只能为数字和英文字母");
                    return;
                }
                executeRequest(RequestUtils.getMLZRegistRequest(mContext, loginName, code, pwdone, pwdtwo, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.i(TGA, "返回的数据:" + jsonObject.toString());
                        try {
                            JSONObject json = new JSONObject(jsonObject.toString());
                            int status = json.getInt("status");
                            if (0 == status) {
                                Intent intent = new Intent(RegistTwoActivity.this, RegistThreeActivity.class);
                                intent.putExtra("loginName", loginName);
                                startActivity(intent);
                            }
                            if (0 != status) {
                                String msg = json.getString("msg");
                                MyCenterUtils.ToastText(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }));

                break;
            case R.id.tv_go_zhuce:
                Intent intent = new Intent(RegistTwoActivity.this, RegistThreeActivity.class);
                enterActivityWithFinish(intent);
                break;
        }
    }
}
